package com.lc.afbx.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.Conn;
import com.lc.afbx.util.MyToast;
import com.zcx.helper.app.AppException;
import com.zcx.helper.http.Http;
import com.zcx.helper.secret.SecretAESDESede;
import com.zcx.helper.util.UtilLog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2016/7/25.
 */
public class PayWebActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_web);

        WebView webView;
        webView = ((WebView) (findViewById(R.id.pay_web)));

        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient());

        webView.setWebViewClient(new WebViewClient());

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    AccidentInsuranceInsureActivity.closeCallback closeCallback = (AccidentInsuranceInsureActivity.closeCallback) getAppCallBack(AccidentInsuranceInsureActivity.class);

                    closeCallback.close();

                } catch (AppException e) {
                    e.printStackTrace();
                }

                BaseApplication.INSTANCE.finishActivity(PayWebActivity.class);
            }
        });


        String ordersn = getIntent().getStringExtra("ordersn");

        String typeTag = getIntent().getStringExtra("type");

        String type = "";

        switch (typeTag) {

            case "1":
                type = "business";
                break;

            case "2":
                type = "compulsory";
                break;

            case "3":
                type = "accident";
                break;

            default:
                MyToast.showToast("保单adapter的type 没有传过来", context);
                break;

        }

        try {
            JSONObject j = new JSONObject();
            j.put("type", type);
            j.put("ordersn", ordersn);

            SecretAESDESede mySecret = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS7PADDING);
            String var19 = Http.getInstance().log(this.getClass().toString() + "->get", j.toString());
            String url = null;
            try {
                url = Conn.SERVICE + Conn.INLET_API_ABCPAY_INDEX + "?Keys=" + URLEncoder.encode(Http.getInstance().log("-------->", mySecret.encrypt(var19)), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            UtilLog.e("---------->", url);

            webView.loadUrl(url);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
